package com.jet.goods.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jet.core.exception.CustomException;
import com.jet.core.model.vo.PageVo;
import com.jet.core.result.ResultCode;
import com.jet.core.utils.IdUtil;
import com.jet.goods.domain.dto.TagDto;
import com.jet.goods.domain.entity.GoodsTag;
import com.jet.goods.domain.query.TagQuery;
import com.jet.goods.mapper.GoodsTagMapper;
import com.jet.goods.service.IGoodsTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-04
 */
@Service
public class GoodsTagServiceImpl extends ServiceImpl<GoodsTagMapper, GoodsTag> implements IGoodsTagService {

    @Override
    public void create(long merchantNo, TagDto dto) {
        if (existsByName(merchantNo, null, dto.getName())) {
            throw new CustomException(ResultCode.BAD_REQUEST, "标签名重复");
        }
        GoodsTag entity = new GoodsTag();
        entity.setId(IdUtil.generateLower());
        entity.setMerchantNo(merchantNo);
        entity.setName(dto.getName().trim());
        entity.setRemark(dto.getRemark());
        entity.setSort(dto.getSort());
        super.save(entity);
    }

    @Override
    public void edit(long merchantNo, String id, TagDto dto) {
        if (existsByName(merchantNo, id, dto.getName())) {
            throw new CustomException("标签名重复");
        }
        GoodsTag entity = new GoodsTag();
        entity.setId(id);
        entity.setName(dto.getName().trim());
        entity.setRemark(dto.getRemark());
        entity.setSort(dto.getSort());
        super.updateById(entity);
    }

    private boolean existsByName(long merchantNo, String id, String name) {
        Wrapper<GoodsTag> wrapper = new LambdaQueryWrapper<GoodsTag>()
                .eq(GoodsTag::getMerchantNo, merchantNo)
                .eq(GoodsTag::getName, name)
                .ne(StrUtil.isNotBlank(id), GoodsTag::getId, id);
        return super.exists(wrapper);
    }

    @Override
    public PageVo<GoodsTag> page(long merchantNo, String name, TagQuery query) {
        Page<GoodsTag> tempQuery = new Page<>(query.getPageIndex(), query.getPageSize());
        QueryWrapper<GoodsTag> wrapper = new QueryWrapper<GoodsTag>();
        wrapper.eq("1", 1);
        wrapper.lambda().and(x -> x.eq(GoodsTag::getMerchantNo, merchantNo).or().eq(GoodsTag::getMerchantNo, 0))
                .like(StrUtil.isNotBlank(name), GoodsTag::getName, name)
                .orderByDesc(GoodsTag::getSort)
                .orderByDesc(GoodsTag::getCreateTime);
        IPage<GoodsTag> page = super.page(tempQuery, wrapper);
        List<GoodsTag> list = page.getRecords();
        if (CollectionUtils.isEmpty(list)) {
            return PageVo.result();
        }
        return PageVo.result(page, list);
    }

    @Override
    public List<GoodsTag> findAll(long merchantNo) {
        Wrapper<GoodsTag> wrapper = new LambdaQueryWrapper<GoodsTag>()
                .eq(GoodsTag::getMerchantNo, merchantNo);
        return super.list(wrapper);
    }
}
